/*************************************************************************
*
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2015 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/

#ifndef PLATFORM_FILESYSTEM
#define PLATFORM_FILESYSTEM

#if __has_include(<filesystem>)
#include <filesystem>
#else

#include "../Platform.h"
#include <string>
#include <memory>
#include <chrono>
#include <system_error>

namespace std
{
    namespace filesystem
    {

        class path;
        struct space_info;
        using file_time_type = chrono::system_clock::time_point;

        // copy from to to and return true on success
        bool copy_file(const path& from, const path& to);

        // create directory, does not create intermediate directories. true if created
        bool create_directory(const path& p);

        // true if file/directory exists at path
        bool exists(const path& p);

        // returns the size of the file. It is an error if the file is a directory
        std::uintmax_t file_size(const path& p);

        // returns true in case item pointed out by path is directory else false
        bool is_directory(const path& p);

        // returns the time of the last modification of the file
        file_time_type last_write_time(const path& p);

        // sets the time of the last modification of the file/directory
        void last_write_time(const path& p, file_time_type new_time);

        // remove file or directory (standard version returns a bool)
        bool remove(const path& p);

        // remove recursively (standard version returns a count)
        void remove_all(const path& p);

        // rename file at old to new
        void rename(const path& old_p, const path& new_p);

        // determines information about the filesystem on which pathname p is located,
        // as if by POSIX statvfs
        space_info space(const path& p);

        // A directory suitable for temporary files. The path is guaranteed to exist and to be a directory. 
        path temp_directory_path();

        class path
        {
            string _p;
        public:
#ifdef _WIN32
            static const char preferred_separator = '\\';
#else
            static const char preferred_separator = '/';
#endif

            // constructors
            path();
            path(const path& p);
            path(const string& p);
            path(path&& p);
            path(string&& p);

            // assignments
            path& operator=(const path& p);
            path& operator=(const string& p);
            path& operator=(path&& p);
            path& operator=(string&& p);

            // appends
            path operator/(const path& src) const;
            path& append(const path& src);

            // modifiers
            path& replace_extension(const path& replacement = path());

            // native format observers
            const char* c_str() const;
            operator string() const;

            // decomposition
            path parent_path() const;
            path filename() const;
            path extension() const;

            // query
            bool empty() const;

            bool operator==(const path& other) const;
            bool operator!=(const path& other) const;
        };


        class directory_entry
        {
            path _path;
        public:
            // constructors and destructor
            directory_entry();
            directory_entry(const directory_entry& d);
            explicit directory_entry(const path& p);
            ~directory_entry();
            
            // modifiers
            directory_entry& operator=(const directory_entry& d);
            void assign(const path& p);
            
            // observers
            const path& path() const;

            bool operator==(const directory_entry& other) const;
            bool operator!=(const directory_entry& other) const;
        };


        class directory_iterator
        {
            class impl;
            unique_ptr<impl> _impl;
        public:
            // constructs the end iterator
            directory_iterator();
            // constructs an iterator at the first entry of directory p
            explicit directory_iterator(const path& p);
            ~directory_iterator();
            

            const directory_entry& operator*() const;
            const directory_entry* operator->() const;
            directory_iterator& operator++();

            bool operator==(const directory_iterator& other) const;
            bool operator!=(const directory_iterator& other) const;

        private:
            directory_iterator(const directory_iterator& rhs);
            directory_iterator& operator=(const directory_iterator& rhs);
        };

        struct space_info {
            std::uintmax_t capacity;       // total size of filesystem in bytes
            std::uintmax_t free;           // free space on filesystem in bytes
            std::uintmax_t available;      // free space available to a non-privileged process
        };


        class filesystem_error : public system_error
        {
        public:
            explicit filesystem_error(const string& msg, error_code ec = make_error_code(errc::operation_not_permitted)) :
            system_error(ec, msg)
            {
            }

            filesystem_error(const string& msg, const path& path1, error_code ec) :
            system_error(ec, msg),
            _path1(path1)
            {
            }

            filesystem_error(const string& msg, const path& path1, const path& path2, error_code ec) :
            system_error(ec, msg),
            _path1(path1),
            _path2(path2)
            {
            }

            virtual ~filesystem_error() noexcept
            {
            }

            filesystem_error(filesystem_error&& right) noexcept :
            system_error(right.code(), right.what()),
            _path1(std::move(right._path1)),
            _path2(std::move(right._path2))
            {
            }

            filesystem_error& operator=(filesystem_error&& rhs) noexcept
            {
                *((system_error *)this) = *(static_cast<system_error *>(&rhs));
                _path1 = std::move(rhs._path1);
                _path2 = std::move(rhs._path2);
                return (*this);
            }

            const path& path1() const noexcept
            {
                return _path1;
            }

            const path& path2() const noexcept
            {
                return _path2;
            }

            filesystem_error(const filesystem_error&) = default;
            filesystem_error& operator=(const filesystem_error&) = default;

        private:
            path _path1;
            path _path2;
        };

    }
}

#endif
#endif
